﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECCIT;
using ECCIT.Security;
using ECCIT.Utilities;
using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;

public partial class GateService_SaveMyProfile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            ResultInfo result = new ResultInfo();
            var lanagerid = Request.Form["langID"] != null ? int.Parse(Request.Form["langID"]) : 1033;
            int? memberid = new AccountController(lanagerid).AccountInfo.ID;
            if (memberid == null) return;
            var txtoldpw = Request.Form["txtoldpw"];
            var txtnewpw = Request.Form["txtnewpw"];
            var txtnewpw1 = Request.Form["txtnewpw1"];
            var checkpw = Request.Form["checkoldpw"];
            var modifypw = Request.Form["modifypw"];
            MemberModel model = new MemberModel(); 
            if (modifypw!=null&&int.Parse(modifypw)==1)//修改密码
            {
                #region 有原密码的情况
                if (int.Parse(checkpw) == 1)//有原密码的情况
                {
                    if (txtoldpw == "" || !new MemberBLL(lanagerid).CheckPwEqual(txtoldpw, (int)memberid))
                    {
                        result.Result = false;
                        result.Message = lanagerid == 2052 ? "旧密码输入错误" : "The old password input error";
                        Response.Write(JsonUtil.Serialize(result));
                        return;

                    }
                }
                #endregion

                if (txtnewpw != "")
                {
                    if (txtnewpw != txtnewpw1)
                    {
                        result.Result = false;
                        result.Message =lanagerid==2052? "两次密码不一致": "The two passwords are inconsistent";
                       
                        Response.Write(JsonUtil.Serialize(result));
                        return;
                    }

                }
                else
                {
                    result.Result = false;
                    result.Message = lanagerid == 2052 ? "新密码不能为空" : "New password can not be empty";
                    Response.Write(JsonUtil.Serialize(result));
                    return;

                }
                model.Password = MD5Crypto.GetMD5Hash(txtnewpw);

            }
            else
            {
                MemberModel memberModel= new MemberBLL(lanagerid).getMemberByID((int) memberid);
                model.Password = memberModel.Password;
                model.AccountName = memberModel.AccountName;
            }
            
       
            var email = Request.Form["txt_email"]; 
           
            var hid_image = Request.Form["hid_image"];
            var username = Request.Form["txt_username"];
            var mobile = Request.Form["txt_mobile"];
            var year = Request.Form["year"];
            var month = Request.Form["month"];
            var day = Request.Form["day"];
            var txt_artist = Request.Form["txt_artist"];
            var Provicelist = Request.Form["Provicelist"];
            var Citylist = Request.Form["Citylist"] ?? string.Empty;
            var mobileisme = Request.Form["chk_mobile"] != null ? true : false;
            var birthisne = Request.Form["chk_birth"] != null ? true : false;
            var chk_StudyAbroad = Request.Form["chk_StudyAbroad"] != null ? "1" : string.Empty;
            var chk_IELTS = Request.Form["chk_IELTS"] != null ? "1" : string.Empty;
            var chk_ArtsAndCulture = Request.Form["chk_ArtsAndCulture"] != null ? "1" : string.Empty;
           
            var selInterest = Request.Form["selInterest"];
            var showimg = Request.Form["hid_image"] != null ? Request.Form["hid_image"] + ".jpg" : string.Empty;
            

                     
            model.MemberImg = hid_image;
            model.Name = username;
            model.Mobile = mobile.Length == 11 ? mobile : string.Empty;
            ;
            if(int.Parse(year)>0&&int.Parse(month)>0&&int.Parse(day)>0)
            {
                model.Birthday = year + "/" + month + "/" + day;
            }
            else
            {
                model.Birthday = "";
            }
            model.City = Citylist != "Select" ? Citylist : "0";
            model.Provice = Provicelist != "Select" ? Provicelist : "0";
            model.FavoriteArtist = txt_artist;
            model.MOBLIEISME = mobileisme;
            model.BIRTHDAYISME = birthisne;
            model.StudyAbroad = chk_StudyAbroad;
            model.IELTS = chk_IELTS;
            model.ArtsAndCulture = chk_ArtsAndCulture;
           
            model.Email = email;
           // model.Name = Request.Form["txtmembername"];

            if(selInterest!="")
            {

                selInterest = selInterest.Substring(0, selInterest.Length - 1);
                var arr = selInterest.Trim().Split(',');
                IList<MemberIntersetsMappingModel> list=new List<MemberIntersetsMappingModel>();
                foreach (var s in arr)
                {
                    var  MappingModel = new MemberIntersetsMappingModel();
                    MappingModel.COL_INTERESTS_ID = int.Parse(s);
                    MappingModel.COL_MEMBER_ID = (int) memberid;
                    list.Add(MappingModel);            
                }
                new MemberIntersetsMappingBLL(lanagerid).Save(list);
            }
            else
            {
                new MemberIntersetsMappingBLL(lanagerid).DeleteAll((int)memberid);
            }
            model.LastLoginTime = DateTime.Now;
            model.ID = memberid;
            result = new MemberBLL(lanagerid).Save(model);
           
            if (result.Result)
            {
                ResultInfo info=new AccountController(lanagerid).ProcessLogin((int) memberid);
                if(info.Result)
                {

                }
                Response.Write(JsonUtil.Serialize(result));

            }


        }
    }
}